using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using Microsoft.Web.Mvc;
using TeamCommunitySolution.Domain.DataLayer.Base;
using TeamCommunitySolution.Modules.Base;
using TeamCommunitySolution.Modules.Base.Requests;

namespace TeamCommunitySolution.Controllers
{
    [Authorize]
    public class TeamController : Controller
    {

        /// <summary>
        /// Team manager instance.
        /// </summary>
        private BaseManager _baseManager = null;

        /// <summary>
        /// Initializes a new instance of the <see cref="TeamController"/> class.
        /// </summary>
        public TeamController()
            : this(MvcApplication.BaseService)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="TeamController"/> class.
        /// </summary>
        /// <param name="service">The service.</param>
        public TeamController(BaseManager manager)
        {
            this._baseManager = manager;
        }

        /// <summary>
        /// Indexes this instance. GET: /Team/
        /// </summary>
        /// <returns>Action Result.</returns>
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// Creates this instance.
        /// </summary>
        /// <returns>Action Result.</returns>
        public ActionResult Create()
        {
            return View("Create");
        }

        /// <summary>
        /// Creates the specified team.
        /// </summary>
        /// <param name="team">The team object.</param>
        /// <returns>Action Result.</returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(Team team)
        {
            SystemUserRequest userRequest = new SystemUserRequest();
            userRequest.Username = User.Identity.Name;

            SystemUser user = this._baseManager.GetSystemUserByUsername(userRequest).SystemUser;

            TeamRequest request = new TeamRequest();
            request.DateFounded = team.DateFounded;
            request.Name = team.Name;
            request.Description = team.Description;
            request.Owner = user;

            this._baseManager.CreateTeam(request);

            return View("OperationCompleted");
        }

        /// <summary>
        /// Edits the specified id. POST: /Team/Edit/5
        /// </summary>
        /// <param name="id">The team id.</param>
        /// <returns>Action Result.</returns>
        [Authorize]
        public ActionResult Edit(string id)
        {
            TeamRequest request = new TeamRequest();
            Team teamObj = this._baseManager.TeamGetById(id.Trim());
            return View("Edit", teamObj);
        }

        /// <summary>
        /// Updates the specified team.
        /// </summary>
        /// <param name="team">The team object.</param>
        /// <returns>Action Result.</returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Update(Team team)
        {
            SystemUser user = this._baseManager.UserGetByUsername(User.Identity.Name);
            user.TeamCreatedList.Add(team);
            this._baseManager.UserSave(user);

            return View("OperationCompleted");
        }

        /// <summary>
        /// Members the search popup view.
        /// </summary>
        /// <param name="id">The team id.</param>
        /// <returns>Partial view result.</returns>
        public PartialViewResult MemberSearchPopupView(string id)
        {
            return PartialView("MemberSearch");
        }

        /// <summary>
        /// Accepts the request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="id">The  team id.</param>
        /// <returns>Action result.</returns>
        [AcceptAjax]
        public ActionResult AcceptRequest(string request, string id)
        {
            this._baseManager.TeamAcceptRequest(request, id);

            SystemUser user = this._baseManager.UserGetById(id);

            return PartialView("InviteAcceptForm", user);
        }

        /// <summary>
        /// Invites the team member.
        /// </summary>
        /// <param name="formattedMemberName">Name of the formatted member.</param>
        /// <param name="teamId">The team id.</param>
        /// <param name="message">The message.</param>
        /// <returns>Action result.</returns>
        [AcceptVerbs(HttpVerbs.Post | HttpVerbs.Get)]
        public ActionResult InviteTeamMember(string formattedMemberName, string teamId, string message)
        {
            SystemUser invitee = this._baseManager.UserFindByFormattedName(formattedMemberName);
            SystemUser inviter = this._baseManager.UserGetByUsername(User.Identity.Name);
            this._baseManager.TeamInviteMember(teamId, inviter.Id, invitee.Id, message);

            return View("OperationComplete");
        }

        /// <summary>
        /// Searches the name.
        /// </summary>
        /// <param name="q">The query string.</param>
        /// <returns>Action Result.</returns>
        [AcceptVerbs(HttpVerbs.Post | HttpVerbs.Get), Authorize]
        public ActionResult SearchName(string q)
        {
            string[] teams = this._baseManager.TeamFindByName(q);

            return Content(string.Join("\n", teams));
        }
    }
}
